<template>
  <div>
    <van-nav-bar
      title="城市"
      :fixed="true"
      z-index="9999"
      @click-left="onClickLeft"
    >
      <template #left>
        <van-icon name="cross" color="#191a1b" size="19" />
      </template>
    </van-nav-bar>
    <div class="hot">
      <div class="hot_title">热门城市</div>
      <ul class="hot_body">
        <li
          v-for="item in hotList"
          :key="item.cityId"
          @click="getCityName(item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <van-index-bar :index-list="indexList" @select="handleIndexBar">
      <div v-for="item in cityList" :key="item.letter">
        <van-index-anchor :index="item.letter" />
        <van-cell
          v-for="data in item.ctiy"
          :key="data.cityId"
          :title="data.name"
          @click="getCityName(data)"
        />
      </div>
    </van-index-bar>
  </div>
</template>

<script>
import http from '@/util/http'
import { Toast } from 'vant'
import Vue from 'vue'
// 导入全状态和方法
import { mapMutations } from 'vuex'
// 引入工具类showTab
import showTab from '@/util/showTab'
Vue.use(Toast)
export default {
  mixins: [showTab],
  data () {
    return {
      cityList: [],
      hotList: []
    }
  },
  computed: {
    indexList () {
      return this.cityList.map((item) => item.letter)
    }
  },
  mounted () {
    http({
      url: 'https://m.maizuo.com/gateway?k=8150139',
      headers: {
        'X-Host': 'mall.film-ticket.city.list'
      }
    }).then((res) => {
      // 调用函数渲染数据
      this.render(res.data.data.cities)
      // 调用函数筛选热门元素
      this.hotRender(res.data.data.cities)
    })
  },
  methods: {
    // 导入全局的方法
    ...mapMutations(['cityChangeName', 'cityChangeId']),
    getCityName (data) {
      // 向管理状态的mutations提交参数
      // 这个是触发cityeChange事件
      // 直接找到的办法
      // this.$store.commit("cityeChange", cityName);
      // 引入map 方法
      this.cityChangeName(data.name) // 传name
      this.cityChangeId(data.cityId) // 传id
      // 并且返回页面
      this.$router.back()
    },
    // 点击左边索引ABC……
    handleIndexBar (data) {
      Toast(data)
      // console.log(Toast);
    },
    // 导航左按钮点击事件
    onClickLeft () {
      this.$router.back()
    },
    // 渲染hot 城市
    hotRender (list) {
      this.hotList = list.filter((item) => item.isHot === 1)
    },
    // 渲染城市
    render (list) {
      const letterArr = []
      const cityArr = []
      for (let i = 'A'.charCodeAt(); i <= 'Z'.charCodeAt(); i++) {
        letterArr.push(String.fromCharCode(i))
      }
      letterArr.forEach((item) => {
        cityArr.push({
          letter: item,
          ctiy: list.filter((data) => {
            return data.pinyin.slice(0, 1).toUpperCase() === item
          })
        })
      })
      this.cityList = cityArr.filter((item) => item.ctiy.length !== 0)
    }
  }
}
</script>

<style lang="scss"  >
$colorText: #797d82;
$colorText1: #191a1b;
.van-index-bar__sidebar {
  .van-index-bar__index {
    padding: 5px 0;
  }
}
.hot {
  width: 100%;
  padding: 15px;
  background: #fff;
  margin-top: 44px;
  .hot_title {
    width: 100%;
    height: 1.1875rem;
    color: $colorText;
    line-height: 1.1875rem;
    font-size: 13px;
  }
  .hot_body {
    color: $colorText1;
    display: flex;
    flex-wrap: wrap;
    li {
      width: 6.25rem;
      height: 30px;
      background: #f4f4f4;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      color: #191a1b;
      margin: 0.4688rem;
    }
  }
}
.van-toast__text {
  background: #191a1b;
  color: #fff;
  padding: 5px 10px;
  border-radius: 5px;
  opacity: 0.5;
}
</style>
